package com.aaa.hospital.controller;

import com.aaa.hospital.pojo.domain.Log;
import com.aaa.hospital.pojo.query.LogQuery;
import com.aaa.hospital.result.AjaxResult;
import com.aaa.hospital.service.LogService;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Author: danghui
 * @Date: 2022/2/12 14:51
 * @Description: TODO
 */
@Controller
@RequestMapping("logSource")
public class LogSourceController {
    @Autowired
    private LogService logService;

    @RequestMapping("download")
    public void download(LogQuery query, HttpServletResponse response)throws Exception{
        AjaxResult ajaxResult = logService.listAllLog(query);
        Map map = (Map) ajaxResult.getData();
        System.out.println(map+"--------------------------------");
        List<Log> currentData = (List<Log>) map.get("currentData");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        String fileName = URLEncoder.encode("日志信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        EasyExcel.write(   response.getOutputStream()  , Log.class    )     .sheet("日志信息").doWrite( currentData);

    }
}
